package org.jeecg.modules.biz.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.biz.response.OwnNoticeResponse;
import org.jeecg.modules.biz.service.INoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/notice")
@Slf4j
public class NoticeController {

    @Autowired
    private INoticeService noticeService;

    /**
     * 查询用户自己的消息
     * <p>
     * /notice/own-notice
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/own-notice")
    public Result<IPage<OwnNoticeResponse>> ownNotice(
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<OwnNoticeResponse> pageList = new Page<>(pageNo, pageSize);
        Result<IPage<OwnNoticeResponse>> result = new Result<>();
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String sysUserId = sysUser.getId();
        pageList = noticeService.ownNotice(pageList, sysUserId);
        result.setResult(pageList);
        result.setSuccess(true);
        return result;
    }

    /**
     * 阅读通知
     * /notice/read-notice
     *
     * @param noticeId
     * @return
     */
    @GetMapping(value = "/read-notice")
    public Result<Void> readNotice(
            @RequestParam(name = "noticeId") String noticeId) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String sysUserId = sysUser.getId();
        return noticeService.readNotice(noticeId,sysUserId);
    }


}
